/**
 * This file is part of Tamariba. Copyright (C) 2011 Michael Vogt <michael@tamariba.eu>, Patrick Hempel
 * <patrick@tamariba.eu>
 * 
 * Tamariba is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * Tamariba is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with Tamariba. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package eu.tamariba.html.user.client.activity;


import java.util.List;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.requestfactory.shared.Receiver;
import com.google.gwt.requestfactory.shared.ServerFailure;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

import eu.tamariba.html.client.managed.request.TaskProxy;
import eu.tamariba.html.user.client.place.UserDisplayPlace;
import eu.tamariba.html.user.client.place.UserDisplayProxyView;
import eu.tamariba.html.user.client.request.UserRequestFactory;


public class UserDisplayActivity extends AbstractActivity {
    private PlaceController placeController;
    private UserDisplayProxyView<TaskProxy, ?> view;
    private UserRequestFactory requests;

    public UserDisplayActivity(PlaceController placeController,
                UserDisplayProxyView<TaskProxy, ?> view, UserRequestFactory requests) {
        this.placeController = placeController;
        this.view = view;
        this.requests = requests;
    }

    @Override
    public void start(AcceptsOneWidget panel, EventBus eventBus) {
        Place place = placeController.getWhere();
        if (place instanceof UserDisplayPlace) {
            UserDisplayPlace displayPlace = (UserDisplayPlace) place;
            
            requests.statusTaskRequest().findAllTasks().fire(new Receiver<List<TaskProxy>>() {
                @Override
                public void onSuccess(List<TaskProxy> response) {
                    if (response.size() > 0) {
                    	StringBuilder status = new StringBuilder();
                    	for (TaskProxy taskProxy : response) {
							status.append(taskProxy.getName().toString());
						}
                        view.setStatus(status.toString());
                    }
                    else {
                        view.setStatus("Order not found");
                    }
                }
                @Override
                public void onFailure(ServerFailure error) {
                    view.setStatus(error.getMessage());
                }
            });
        }
        panel.setWidget(view);
    }
}